package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.EvaluateCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.Evaluate;
import com.engineering.elink.domain.VideoPraise;
import com.engineering.elink.service.EvaluateService;
import com.engineering.elink.skilldao.SkillMapper;
import com.engineering.elink.util.CollectionUtil;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.CourseMapper;
import com.engineering.elink.videodao.EnrollMapper;
import com.engineering.elink.videodao.EvaluateMapper;
import com.engineering.elink.videodao.VideoPraiseMapper;

@Service
public class EvaluateServiceImpl implements EvaluateService{

	@Autowired
	private EvaluateMapper evaluateMapper;
	
	@Autowired
	private CourseMapper courseMapper;
	
	@Autowired
	private EnrollMapper enrollMapper;
	
	@Autowired
	private VideoPraiseMapper videoPraiseMapper;
	
	@Autowired
	private SkillMapper skillMapper;
	
	@Override
	public Map<String, Object> getTopEvaluatesByCourseId(EvaluateCo co,int type) {
		
		Map<String , Object> map= new HashMap<String, Object>();
		
		map.put("list",evaluateMapper.selectTopEvaluatesByCourseId(co,type));
		map.put("Pagination", new Pagination(co, evaluateMapper.selectTopEvaluatesCount(co,type)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
	}
	
	@Override
	public Map<String, Object> getAllEvaluatesByTopId(BaseCo co,Integer topId,Integer type,boolean parentFlag) {
		Map<String , Object>map= new HashMap<String, Object>();
		
		map.put("list",evaluateMapper.selectAllEvaluatesByTopId(co,topId,type,parentFlag));
		map.put("Pagination", new Pagination(co,evaluateMapper.selectCount(topId,type,parentFlag)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
	}

	@Override
	public Map<String, Object> addPraiseEvaluate(Integer evalCommentId,Integer praiseType,String userId,Integer type) {
		int count = videoPraiseMapper.select(evalCommentId,praiseType,userId,type);
		if(count == 0){
			Evaluate c  = evaluateMapper.selectByPrimaryKey(evalCommentId);
			if(c != null){
				videoPraiseMapper.insertSelective(new VideoPraise(evalCommentId,praiseType,userId,type));
			
				updateEvaluate(praiseType,c);
				
				//更新上级评价的顶/踩数量
				List<Evaluate> eList=evaluateMapper.selectAllEvaluateByRootId(c.getEvaluateId());
				if(CollectionUtil.collectionIsNotEmpty(eList)){
					for(Evaluate e:eList){
						updateEvaluate(praiseType,e);
					}
				}
				
				return ResJsonUtil.getMap(ErrorType.SUCCESS,c);
			}else{
				return ResJsonUtil.getMap(ErrorType.EVALUATE_PRAISE_ERROR);
			}	
		}else {
			return ResJsonUtil.getMap(ErrorType.PRAISE_ERROR);
		}
		
	}
	
	private void updateEvaluate(Integer praiseType,Evaluate c){
		//更新评价的顶/踩数量
		if(praiseType.intValue()==1){//顶
			c.setPraiseCount(c.getPraiseCount()+1);
		}else{//踩
			c.setStampCount(c.getStampCount()+1);
		}
		evaluateMapper.updateByPrimaryKeySelective(c);
	}
	
	@Override
	public Map<String, Object> getEvaluateForMe(BaseCo co,String userId,int type) {
		if(type==0){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,courseMapper.getEvaluateForMe(co, userId));
		}else if(type==1){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,skillMapper.getEvaluateForMe(co, userId));
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}
	
	@Override
	public Map<String, Object> getMyEvaluate(BaseCo co,String userId,int type) {
		if(type==0){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,courseMapper.getMyEvaluate(co, userId));
		}else if(type==1){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,skillMapper.getMyEvaluate(co, userId));
		}
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}
	
}
